import 'dart:ui';

import 'package:demo_flutter_module/sec_page.dart';
import 'package:demo_flutter_module/three_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 5/27/21.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  String? _routName;

  int _flag = 1;

  @override
  void initState() {
    super.initState();
    _routName = window.defaultRouteName;
    receiveMessage();
  }

  @override
  Widget build(BuildContext context) {
    if(_flag==2){
      return SecPage();
    }else if(_flag==3){
      return ThreePage();
    }
    return Scaffold(
      appBar: AppBar(
        title: Text("首页面"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
                onPressed: () {
                  Navigator.of(context)
                      .push(MaterialPageRoute(builder: (BuildContext context) {
                    return SecPage();
                  }));
                },
                child: Text("打开Sec页面")),
            ElevatedButton(
                onPressed: () {
//                sendMessage("{'method':'test','ontent':'flutter 中的数据','code':100}");
                  sendMessage({
                    "method": "test",
                    "ontent": "flutter 中的数据",
                    "code": 100
                  });
                },
                child: Text(
                  "向原生中发送消息",
                  style: TextStyle(color: Colors.white),
                )),
            Text("原生页面传入的参数为[${_routName}]"),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  String recive = "暂无";
  static const BasicMessageChannel messageChannel = const BasicMessageChannel(
      'flutter_and_native_100', StandardMessageCodec());

  //点击按钮发送消息
  Future<Map> sendMessage(Map json) async {
    Map reply = await messageChannel.send(json);

    //解析 原生发给 Flutter 的参数
    int code = reply["code"];
    String message = reply["message"];

    setState(() {
      recive = "code:$code message:$message";
    });
    return reply;
  }

  //接收消息监听
  //initState() 中注册
  void receiveMessage() {
    messageChannel.setMessageHandler((result) async {
      //解析 原生发给 Flutter 的参数
      int code = result["code"];
      String message = result["message"];
      int flag = result["flag"];

      setState(() {
        _flag = flag;
        recive = "receiveMessage: code:$code message:$message";
      });

      return 'Flutter 已收到消息';
    });
  }
}
